
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 05/19/2012 11:30:53
-- Generated from EDMX file: C:\Users\RichardGo\Documents\Visual Studio 2010\Projects\T4DemoOne\T4Demo2\BasicModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [t4DemoDB];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- Dropping existing FOREIGN KEY constraints

IF OBJECT_ID(N'[dbo].[FK_PersonAddress]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Addresses] DROP CONSTRAINT [FK_PersonAddress];
GO
IF OBJECT_ID(N'[dbo].[FK_OrderProduct]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Orders] DROP CONSTRAINT [FK_OrderProduct];
GO
IF OBJECT_ID(N'[dbo].[FK_PersonOrder]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Orders] DROP CONSTRAINT [FK_PersonOrder];
GO
IF OBJECT_ID(N'[dbo].[FK_OrderAddress]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Orders] DROP CONSTRAINT [FK_OrderAddress];
GO

-- Dropping existing tables

IF OBJECT_ID(N'[dbo].[People]', 'U') IS NOT NULL
    DROP TABLE [dbo].[People];
GO
IF OBJECT_ID(N'[dbo].[Products]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Products];
GO
IF OBJECT_ID(N'[dbo].[Orders]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Orders];
GO
IF OBJECT_ID(N'[dbo].[Addresses]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Addresses];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

CREATE TABLE [dbo].[People] (
    [Id] int IDENTITY(1,1)NOT NULL,
    [Name] nvarchar(max) NOT NULL,
    [Phone] nvarchar(max) NOT NULL
);
GO

CREATE TABLE [dbo].[Products] (
    [Id] int IDENTITY(1,1)NOT NULL,
    [Name] nvarchar(max) NOT NULL,
    [SKU] nvarchar(max) NOT NULL
);
GO

CREATE TABLE [dbo].[Orders] (
    [Id] int IDENTITY(1,1)NOT NULL,
    [Date] datetime NOT NULL,
    [ProductId] int NOT NULL,
    [PersonId] int NOT NULL,
    [AddressId] int NOT NULL
);
GO

CREATE TABLE [dbo].[Addresses] (
    [Id] int IDENTITY(1,1)NOT NULL,
    [Line1] nvarchar(max) NOT NULL,
    [City] nvarchar(max) NOT NULL,
    [State] nvarchar(max) NOT NULL,
    [Zip] nvarchar(max) DEFAULT N'30040' NOT NULL,
    [PersonId] int NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

ALTER TABLE [dbo].[People]
ADD CONSTRAINT [PK_People]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

ALTER TABLE [dbo].[Products]
ADD CONSTRAINT [PK_Products]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

ALTER TABLE [dbo].[Orders]
ADD CONSTRAINT [PK_Orders]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

ALTER TABLE [dbo].[Addresses]
ADD CONSTRAINT [PK_Addresses]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

ALTER TABLE [dbo].[Addresses]
ADD CONSTRAINT [FK_PersonAddress]
    FOREIGN KEY ([PersonId])
    REFERENCES [dbo].[People]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

CREATE INDEX [IX_FK_PersonAddress]
ON [dbo].[Addresses]
    ([PersonId]);
GO

ALTER TABLE [dbo].[Orders]
ADD CONSTRAINT [FK_OrderProduct]
    FOREIGN KEY ([ProductId])
    REFERENCES [dbo].[Products]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

CREATE INDEX [IX_FK_OrderProduct]
ON [dbo].[Orders]
    ([ProductId]);
GO

ALTER TABLE [dbo].[Orders]
ADD CONSTRAINT [FK_PersonOrder]
    FOREIGN KEY ([PersonId])
    REFERENCES [dbo].[People]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

CREATE INDEX [IX_FK_PersonOrder]
ON [dbo].[Orders]
    ([PersonId]);
GO

ALTER TABLE [dbo].[Orders]
ADD CONSTRAINT [FK_OrderAddress]
    FOREIGN KEY ([AddressId])
    REFERENCES [dbo].[Addresses]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

CREATE INDEX [IX_FK_OrderAddress]
ON [dbo].[Orders]
    ([AddressId]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------